using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GotchaManagerUI : MonoBehaviour
{
	public static GotchaManagerUI instance;

	[HideInInspector]
	public UIRoot uiRoot;

	[HideInInspector]
	public GotchaChestUI selectedChest;

	public List<GotchaReward> selectedChestActualRewards;

	public GameObject gotchaHolder;

	public GotchaChestUI gotchaChestUIexample;

	public Transform chestsGtid;

	public GotchaRewardList rewardListUI;

	public UIPanel blockPanel;

	private GotchaChestUI WoodenChestUI;

	private GotchaChestUI BronzeChestUI;

	private GotchaChestUI SilvernChestUI;

	private GotchaChestUI GoldenChestUI;

	public bool initialized;

	private GotchaChestUI lastSelectedChest;

	public GameObject rewardPanel;

	public GotchaRewardUI resultRewardUI;

	private GotchaReward resultReward;

	private Coroutine hideRewardPanelWithDelay;

	public static event Action OnHideGotcha;

	public static IEnumerator CreateInstance()
	{
		LoadingPanel.instance.ShowLoadingPanel(true);
		ResourceRequest resourceRequest = Resources.LoadAsync("Prefabs/GotchaUI");
		yield return resourceRequest;
		GameObject tmp = UnityEngine.Object.Instantiate(resourceRequest.asset) as GameObject;
		tmp.transform.SetParent(GameUI.instance.transform);
		tmp.transform.localScale = Vector3.one;
		tmp.GetComponent<GotchaManagerUI>().Init();
		LoadingPanel.instance.ShowLoadingPanel(false);
	}

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
		Init();
		if (BCWStorage.Get<string>("new_lobby_tutorial").Equals("Ended"))
		{
			InGameTutorial.StartTutorial("new_tutorial_gotcha", controllerMenu.thisScript.panelMainMenu.transform, string.Empty);
		}
	}

	public void Init()
	{
		if (!initialized)
		{
			CoroutineRunner.Instance.StartCoroutine(Initialization());
		}
	}

	private IEnumerator Initialization()
	{
		initialized = true;
		while (!JSONManager.Instance.ServerTimeLoad())
		{
			yield return null;
		}
		uiRoot = NGUITools.FindInParents<UIRoot>(base.gameObject);
		WoodenChestUI = SetChestUI(GotchaChestType.Wooden);
		BronzeChestUI = SetChestUI(GotchaChestType.Bronze);
		SilvernChestUI = SetChestUI(GotchaChestType.Silvern);
		GoldenChestUI = SetChestUI(GotchaChestType.Golden);
	}

	public void ShowGotchaUI()
	{
		if (!JSONManager.Instance.ServerTimeLoad())
		{
			controllerMenu.thisScript.ShowNoInternet();
			return;
		}
		UnselectChest();
		gotchaHolder.SetActive(true);
		WoodenChestUI.Select(true);
		if (GotchaManager.instance.gotLastSpinReward)
		{
			resultReward = GotchaManager.instance.lastSpinReward;
			ShowRewardPanel();
		}
	}

	public void HideGotchaUI()
	{
		gotchaHolder.SetActive(false);
		rewardListUI.gameObject.SetActive(false);
		if (GotchaManagerUI.OnHideGotcha != null)
		{
			GotchaManagerUI.OnHideGotcha();
		}
		UnselectChest();
		GotchaShowButton.instance.SetUI();
	}

	private GotchaChestUI SetChestUI(GotchaChestType chestType)
	{
		gotchaChestUIexample.gameObject.SetActive(false);
		GotchaChestUI gotchaChestUI = UnityEngine.Object.Instantiate(gotchaChestUIexample);
		gotchaChestUI.transform.SetParent(chestsGtid);
		gotchaChestUI.transform.localScale = Vector3.one;
		gotchaChestUI.Init(chestType);
		gotchaChestUI.gameObject.SetActive(true);
		return gotchaChestUI;
	}

	public void SelectChest(GotchaChestUI gotchaChestUI = null)
	{
		if (!(selectedChest == gotchaChestUI))
		{
			if (selectedChest != null)
			{
				selectedChest.Select(false);
			}
			selectedChest = gotchaChestUI;
			selectedChestActualRewards.Clear();
			selectedChestActualRewards = selectedChest.gotchaChest.GetChestRewards();
			ShowRewardsPreview();
		}
	}

	private void UnselectChest()
	{
		lastSelectedChest = selectedChest;
		WoodenChestUI.Select(false);
		BronzeChestUI.Select(false);
		SilvernChestUI.Select(false);
		GoldenChestUI.Select(false);
		selectedChest = null;
		selectedChestActualRewards.Clear();
		blockPanel.gameObject.SetActive(false);
		rewardListUI.HideScroll();
	}

	public void ShowRewardsPreview()
	{
		if (selectedChest == null && selectedChest.type != 0 && selectedChestActualRewards.Count > 0)
		{
			Debug.LogError("Chest not selected!");
		}
		else
		{
			CoroutineRunner.Instance.StartCoroutine(rewardListUI.SetPreviewScroll(selectedChestActualRewards));
		}
	}

	public void Spin()
	{
		if (selectedChest != null && selectedChest.type == GotchaChestType.Non)
		{
			Debug.LogError("Chest not selected!");
			return;
		}
		if (selectedChestActualRewards == null || selectedChestActualRewards.Count == 0)
		{
			Debug.LogError("Bad actual reward list!");
			return;
		}
		resultReward = null;
		resultReward = GotchaManager.instance.GetSpinReward(selectedChestActualRewards, selectedChest.gotchaChest);
		if (resultReward == null)
		{
			Debug.LogError("No result reward!");
			selectedChest.gotchaChest.Open();
			return;
		}
		GotchaManager.instance.gotLastSpinReward = true;
		GotchaManager.instance.lastSpinReward = resultReward;
		blockPanel.gameObject.SetActive(true);
		CoroutineRunner.Instance.StartCoroutine(rewardListUI.SpinEnumerator(resultReward));
		selectedChest.gotchaChest.ClearActualRewards();
	}

	public void ShowRewardPanel()
	{
		BCWAudioResource.Instance.PlaySound("SpinRewardClip");
		if (selectedChest != null)
		{
			selectedChest.UpdateTimerAndUI();
		}
		bool flag = resultReward.type == GotchaRewardType.Product && MarketController.Instance.GetProduct(resultReward.id) is ProductFreeUpgrade;
		blockPanel.gameObject.SetActive(true);
		resultRewardUI.Set(resultReward, !flag, false);
		rewardPanel.SetActive(true);
		GotchaManager.instance.gotLastSpinReward = false;
		resultReward.GiveReward();
		resultReward = null;
		if (hideRewardPanelWithDelay != null)
		{
			CoroutineRunner.Instance.StopCoroutine(hideRewardPanelWithDelay);
		}
	}

	public IEnumerator HideRewardPanelWithDelay(float delay)
	{
		yield return new WaitForSeconds(delay);
		HideRewardPanel();
	}

	public void HideRewardPanel()
	{
		if (!BCWStorage.Get<string>("new_lobby_tutorial").Equals("Ended"))
		{
			InGameTutorial.StartTutorial("new_tutorial_gotcha", controllerMenu.thisScript.panelMainMenu.transform, "GotchaEnd");
		}
		blockPanel.gameObject.SetActive(false);
		rewardPanel.SetActive(false);
		UnselectChest();
		if (lastSelectedChest != null && lastSelectedChest.type != 0 && lastSelectedChest.gotchaChest.count > 0)
		{
			lastSelectedChest.Select(true);
		}
		else
		{
			WoodenChestUI.Select(true);
		}
		if (hideRewardPanelWithDelay != null)
		{
			CoroutineRunner.Instance.StopCoroutine(hideRewardPanelWithDelay);
		}
	}

	private void LogReward(GotchaReward resultReward)
	{
		string text = " ************** Reward is ";
		switch (resultReward.type)
		{
		case GotchaRewardType.Product:
			if (resultReward.amount > 0)
			{
				Debug.Log(text + "PRODUCT id: " + resultReward.id + " amount: " + resultReward.amount);
			}
			else
			{
				Debug.Log(text + "PRODUCT id: " + resultReward.id);
			}
			break;
		case GotchaRewardType.Cash:
			Debug.Log(text + "CASH amount: " + resultReward.amount);
			break;
		case GotchaRewardType.Gold:
			Debug.Log(text + "GOLD amount: " + resultReward.amount);
			break;
		}
	}
}
